import { Component, Vue } from 'vue-property-decorator'
import BatchChangeUsedStatus from './BatchChangeUsedStatus.vue'
import { subsidyServices } from '@/common/services/subsidy'

@Component
export class BatchChangeUsedStatusMixin extends Vue {
  public currentAreaId!: string

  public batchChangeUsedLoading = true
  public handlerBatchChangeStatus() {
    const self = this
    this.$dialog(BatchChangeUsedStatus, {
    },
    {
      title: '批量修改当前行政区下补贴状态',
      width: '400px',
      maskClosable: false,
      confirmLoading: this.batchChangeUsedLoading,
      on: {
        async ok () {
          // @ts-ignore
          const status = this.$refs._component.selectUsedStatus
          if (!status) return false;
          await self.handlerUpdateUsedStatus(status);
          return true;
        },
        cancel () {
          console.log('cancel 回调')
        },
        close () {
          console.log('modal close 回调')
        }
      }
    })
  }

  public handlerUpdateUsedStatus(selectUsedStatus: string) {
    if (!selectUsedStatus) {
      this.$message.error('请选择批量操作的补贴状态！')
      return
    }
    this.batchChangeUsedLoading = true
    return subsidyServices
      .setSubsidyStatus({
        is_area: true,
        s_a_id: this.currentAreaId,
        is_used: selectUsedStatus
      })
      .then(() => {
        // @ts-ignore
        this.getData()
        this.$message.success('补贴状态更新成功')
      })
      .finally(() => {
        this.batchChangeUsedLoading = false
      })
  }
}
